import Vue from 'vue'
import Vuex from 'vuex'
import $ from 'jquery'
import axios from 'axios'

Vue.use(Vuex)
var that = this;

export default new Vuex.Store({
  state: {
    logo: '',
    name: '',
    info: '',
    time: '',
    fee: '',
    bg: '',
    color: '',
    icon: '',
    description: '',
    activity: '',
    list: '',
    minimum_order_amount: '',
    rating: '',
    service_score: '',
    food_score: '',
    comments: '',
    tag: '',
    compare_rating: '',
    offset: 0,
    tag_name: '全部',
    id: '',
    count:[],
    total:'',
    num:[],
  },
  getters: {},
  mutations: {
    BACK: function() {
      history.back()
    },
    SWITCHPAGE: function(state, page) {
      state.page = page
      if(page == 1) {
        $('nav div').eq(0).addClass('tap').siblings().removeClass();
        $('.allgoods').addClass('show');
        $('.comment').removeClass('show');
        $('footer').css('display', 'block');
      } else {
        $('nav div').eq(1).addClass('tap').siblings().removeClass();
        $('.comment').addClass('show');
        $('.allgoods').removeClass('show');
        $('footer').css('display', 'none');
      }
    },
    CLICKTAG: function(state,index) {
        $('.tabb li').eq(index).addClass('select').siblings().removeClass();
        $('.goodslist').animate({
              scrollTop:$('dl').eq(index).position().top-$('dl').eq(0).position().top
            }, 500)
      },
    CHECK: function(state) {
      $('.tags').on('click', 'li', function(e) {
        $(this).addClass('current').siblings().removeClass('current');
        state.tag_name = $(this).children().text();
        axios.get('http://localhost:3000/shop/comment?offset=0&tag_name=' + state.tag_name + '&id=' + state.id).then(function(res) {
          console.log(res.data);
          var data = res.data;
          state.comments = data

        }).catch(function(err) {
          console.log(err);
        })

      })
    },
    ADD: function(state,obj) {
        var dlArr=document.getElementsByTagName('dl');
        var count=dlArr[obj.index].getElementsByClassName('count')[obj.i];
        var minus=dlArr[obj.index].getElementsByClassName('minus-count')[obj.i];
        var price=dlArr[obj.index].getElementsByClassName('price')[obj.i].innerHTML;
        var total=document.getElementsByClassName('total')[0];
        var currentCount=document.getElementsByClassName('tabb')[0];
        var currentCountArr=currentCount.getElementsByClassName('currentcount');
        var afterNode=document.getElementsByClassName('after')[0];
        $('.currentcount').eq(obj.index).css('display','block');
        $('.shopping-cart').removeClass('empty');
        afterNode.style.display='block';
        count.style.display='inline-block';
        minus.style.display='inline-block';
        count.innerHTML++;
        currentCountArr[obj.index].innerHTML++;
        afterNode.innerHTML++;
  
        
    },
    MINUS:function(state,obj){
        var dlArr=document.getElementsByTagName('dl');
        var count=dlArr[obj.index].getElementsByClassName('count')[obj.i];
        var minus=dlArr[obj.index].getElementsByClassName('minus-count')[obj.i];
        var price=dlArr[obj.index].getElementsByClassName('price')[obj.i].innerHTML;
        var currentCount=document.getElementsByClassName('tabb')[0];
        var currentCountArr=currentCount.getElementsByClassName('currentcount');
        var afterNode=document.getElementsByClassName('after')[0];
        if(count.innerHTML<2){
          count.style.display='none';
          minus.style.display='none';
        }   
        if(currentCountArr[obj.index].innerHTML<2){
          currentCountArr[obj.index].style.display='none'
        } 
        if(afterNode.innerHTML<2){
          afterNode.style.display='none';
          $('.shopping-cart').addClass('empty');
        } 
          count.innerHTML--;
          currentCountArr[obj.index].innerHTML--;
          afterNode.innerHTML--;
    },
    ADDLIST: function(state) {

      $('.size').css('display', 'block').addClass('move');
      $('.black').css('display', 'block').addClass('on');

    },
    CLOSE: function(state) {
      $('.size').removeClass('move').css('display', 'none');
      $('.black').removeClass('on').css('display', 'none');

    },
    ALLDATA: function(state) {
      state.list = that.list;
      state.id = that.id

    },
    ALLCOMMENTS: function(state) {
      state.comments = that.comments;
    },
    ALLACTIVITIES: function(state) {
      state.logo = that.imgSrc;
      state.bg = that.bg
      state.name = that.name;
      state.time = that.time;
      state.fee = that.fee;
      state.info = that.info;
      state.icon = that.iconName;
      state.color = that.iconColor;
      state.description = that.description;
      state.activity = that.activity;
      state.minimum_order_amount = that.minimum_order_amount;
      state.rating = that.rating;
    },
    RATING: function(state) {
      state.service_score = that.service_score;
      state.food_score = that.food_score;
      state.compare_rating = that.compare_rating;
    },
    TAG: function(state) {
      state.tag = that.tag

    },
    LOADMORE: function(state) {
      state.offset += 10;
      axios.get('http://localhost:3000/shop/comment?offset=' + state.offset + '&tag_name=' + state.tag_name + '&id=' + state.id).then(function(res) {
        console.log(res.data);
        var data = res.data;
        state.comments = state.comments.concat(data)

      }).catch(function(err) {
        console.log(err);
      })
    }
  },
  actions: {
    ALLDATA(context, id) {
      axios.get('http://localhost:3000/shop?id=' + id).then(function(res) {
        console.log(res.data);
        that.list = res.data;
        that.id = id
        console.log(id);
        context.commit('ALLDATA')
      }).catch(function(err) {
        console.log(err);
      })
    },
    ALLACTIVITIES(context, id) {
      axios.get('http://localhost:3000/shop/head?id=' + id).then(function(res) {

        that.name = res.data.name;
        that.info = res.data.promotion_info;
        that.time = res.data.order_lead_time;
        that.fee = res.data.piecewise_agent_fee.description;
        that.img1 = res.data.image_path.substr(0, 1);
        that.img2 = res.data.image_path.substr(1, 2);
        that.img3 = res.data.image_path.substr(3);
        that.imgS = that.img1 + '/' + that.img2 + '/' + that.img3;
        that.iconName = res.data.activities[0].icon_name;
        that.iconColor = res.data.activities[0].icon_color;
        that.description = res.data.activities[0].description;
        that.activity = res.data.activities.length;
        that.minimum_order_amount = res.data.float_minimum_order_amount;
        that.rating = res.data.rating;
        console.log(res.data.float_minimum_order_amount)
        if(res.data.image_path.substr(res.data.image_path.length - 2, 1) == 'n') {
          that.imgSrc = 'https://fuss10.elemecdn.com/' + that.imgS + '.png?imageMogr/format/webp/thumbnail/!130x130r/gravity/Center/crop/130x130/';
          that.bg = 'https://fuss10.elemecdn.com/' + that.imgS + '.png?imageMogr/format/webp/thumbnail/!40p/blur/50x40/'
        } else {
          that.imgSrc = 'https://fuss10.elemecdn.com/' + that.imgS + '.jpeg?imageMogr/format/webp/thumbnail/!130x130r/gravity/Center/crop/130x130/';
          that.bg = 'https://fuss10.elemecdn.com/' + that.imgS + '.jpeg?imageMogr/format/webp/thumbnail/!40p/blur/50x40/'
        }

        context.commit('ALLACTIVITIES')
      }).catch(function(err) {
        console.log(err);
      })
    },
    ALLCOMMENTS(context, id) {
      axios.get('http://localhost:3000/shop/comment?offset=0&tag_name=全部&id=' + id).then(function(res) {
        console.log(res.data);
        that.comments = res.data
        context.commit('ALLCOMMENTS')
      }).catch(function(err) {
        console.log(err);
      })
    },
    RATING(context, id) {
      axios.get('http://localhost:3000/shop/rating/?id=' + id).then(function(res) {
        that.food_score = res.data.food_score.toFixed(1);
        that.service_score = res.data.service_score.toFixed(1);
        that.compare_rating = res.data.compare_rating;
        context.commit('RATING')
      }).catch(function(err) {
        console.log(err);
      })
    },
    TAG(context, id) {
      axios.get('http://localhost:3000/shop/tag?id=' + id).then(function(res) {
        that.tag = res.data;
        console.log(that.tag);
        context.commit('TAG')
      }).catch(function(err) {
        console.log(err);
      })
    }
  }
})